/*
  Copyright 2007 Anders Båtstrand

      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
      You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.

 */

package org.dyndns.anderlin.jmplayer.player;

import org.dyndns.anderlin.jmplayer.model.Track;

import javax.swing.*;
import java.util.ArrayList;
import java.util.List;

public abstract class AbstractPlayer extends DefaultBoundedRangeModel {

    private PlayerState state;
    private List<PlayerListener> listeners;

    protected Track song;

    public AbstractPlayer() {
        listeners = new ArrayList<PlayerListener>();
    }

    protected void setState(PlayerState state) {
        this.state = state;
    }

    public void setSong(Track song) {
        this.song = song;
        if (song == null) {
            setState(PlayerState.NO_SONG);

        } else {
            setState(PlayerState.STOPPED);
            setMaximum(song.getDuration().intValue());
        }

    }

    protected void endOfFile() {
        setSong(null);
    }

    public abstract void play() throws Exception;

    public abstract void resume();

    public abstract void pause();

    public abstract void stop();

    public void addPlayerListener(PlayerListener listener) {
        listeners.add(listener);
    }

    public void removePlayerListener(PlayerListener listener) {
        listeners.remove(listener);
    }

    protected void fireStateChanged() {
        for (PlayerListener listener : listeners) {
            listener.stateChanged(state);
        }
    }

}
